<style src="./index.scss" lang="scss" scoped rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
import apiConfig from "./apiConfig";
export default {
    mixins: [Vue.prototype.$vFN.Tools],
    components: {
        QRCode: async () =>
            Vue.extend(await Vue.prototype.$vFN.Components.Base.QRCode()),
        ImageBox: async () =>
            Vue.extend(await Vue.prototype.$vFN.Components.Base.ImageBox()),
    },
    props: {
        // 接口列表
        apiConfig: {
            type: Object,
            default: apiConfig,
        },
        // 使用者window实例 必定义
        appWindow: {
            type: Object,
            default: null,
        },
    },
    computed: {},
    data() {
        return {
            webUrl: "",
            wxappErrorMessage: "",
            wxappUrl: "",
            

            wxappLoading: true,
            wxappLoad: true,
            // 配置数据
            config: false,
        };
    },
    methods: {
        // 生成唯一标识
        async getGenerateCode(v) {
            if (this.$vFN.Utils.isEmpty(v)) {
                return "";
            }
            let res = await this.$vFN.Http.post(
                {
                    url: this.apiConfig.generateCode.url,
                    domain: this.apiConfig.generateCode.domain,
                    data: {
                        qrcodeScene: v,
                    },
                },
                this.appWindow
            );
            if (res._ok) {
                return res.data.id;
            } else {
                return "";
            }
        },
        // 获取小程序分享码
        // scene 分享内容  width 宽度
        async getWxAppShareCode(data_) {
            let _config = {
                scene: 0,
                width: 500,
            };
            if (!this.$vFN.Utils.isEmpty(data_)) {
                if (!this.$vFN.Utils.isEmpty(data_.scene)) {
                    _config.scene = data_.scene;
                }
                if (!this.$vFN.Utils.isEmpty(data_.width)) {
                    _config.width = data_.width;
                }
            }

            let res = await this.$vFN.Http.post(
                {
                    url: this.apiConfig.getUnlimited.url,
                    domain: this.apiConfig.getUnlimited.domain,
                    isErrorAlert: false,
                    data: {
                        width: _config.width,
                        page: "pages/index/index",
                        hyaline: true,
                        lineColor: {
                            r: 0,
                            g: 0,
                            b: 0,
                        },
                        autoColor: false,
                        scene: _config.scene,
                    },
                },
                this.appWindow
            );
            if (res.code == 200) {
                return {
                    code: res.data,
                };
            } else {
                return {
                    message: res.data.message,
                };
            }
        },
        // 获取小程序二维码
        async getWxappCode() {
            let _scene = {};

            this.wxappLoading = true;

            if (!this.$vFN.Utils.isEmpty(this.config)) {
                let _url = "";
                 if (!this.$vFN.Utils.isEmpty(this.config.path)) {
                  _url += this.config.path;  
                 }

                let _queryStr = this.$vFN.Utils.urlEncode(this.config.query);

                if (!this.$vFN.Utils.isEmpty(_queryStr)) {
                    _url += "?" + _queryStr;
                }

                if (!this.$vFN.Utils.isEmpty(_url)) {
                    _scene.scene = await this.getGenerateCode(_url);
                }
            }

            let res = await this.getWxAppShareCode(_scene);
            console.error(
                222,
                res,
                this.$vFN.Utils.urlEncode(this.config.query)
            );
            if (res.code) {
                this.wxappUrl = res.code;
            } else {
                this.wxappErrorMessage = res.message;
            }
            this.wxappLoading = false;
        },
        // 获取H5微信公众号二维码链接
        getWebCode() {
            let _d = this.$vFN.Tools.getShopLocationUrl(this.config);
            console.error("_d", _d);
            this.webUrl = _d;
        },
        init(params) {
            if (this.$vFN.Utils.isEmpty(params)) {
                params = {
                    config: {
                        query:{}
                    },
                };
            } else {
                if (this.$vFN.Utils.isEmpty(params.config)) {
                    params.config = {
                        query:{}
                    };
                }
            }

            if (JSON.stringify(params.config) != JSON.stringify(this.config)) {
                this.config = params.config;
                console.error("获取", this.config);
                this.getWebCode();
                this.getWxappCode();
            }
        },
    },
    watch: {
        // desFormData: {
        //     handler() {
        //         if (this.desFormData) {
        //             this.updateDesData();
        //             this.getMiniAppCode();
        //         }
        //     },
        //     immediate: true,
        //     deep: true,
        // },
    },
    mounted() {},
    beforeCreate() {},
};
</script>
